﻿using ConfExpert.Forms.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevTeam;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;

namespace ConfExpert.Forms.Depo.Westbrook
{
    public partial class WB_BOMRevize : XPopupForm
    {
        public string idx { get; set; }
        public DataSet dsBOMLines { get; set; }
        public WB_BOMRevize(string idx, DataSet dsBOMLines)
        {
            InitializeComponent();
            this.idx = idx;
            this.dsBOMLines = dsBOMLines;
        }

        private void WB_BOMOlustur_Load(object sender, EventArgs e)
        {
            gvBOMLines.Init(dsBOMLines.Tables[1]);

            gvBOMLines.Columns["Tip"].ColumnEdit = GetCustomRI("Tip", false, "Standart", "RenkleDegisen");
            gvBOMLines.Columns["Qty"].DisplayFormat.FormatString = "#,##0.###";

            gvBOMLines.MakeReadOnly("Tip", "AksesuarId", "Seri", "Qty");


            emptyEditor = new RepositoryItemButtonEdit();
            emptyEditor.Buttons.Clear();
            emptyEditor.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.HideTextEditor;
            gcBOMLines.RepositoryItems.Add(emptyEditor);

            gvBOMLines.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            bbiSave.ItemClick += bbiSave_ItemClick;
            gvBOMLines.RowCellStyle += gvBOMLines_RowCellStyle;
            gvBOMLines.ShowingEditor += gvBOMLines_ShowingEditor;
            gvBOMLines.CustomRowCellEdit += gvBOMLines_CustomRowCellEdit;
            gvBOMLines.GetManager().Pasting += WB_BOMOlustur_Pasting;

            gvBOMLines.AddSummary();
            gvBOMLines.BestFitColumns();
        }

        void WB_BOMOlustur_Pasting(int rowHandle, DevExpress.XtraGrid.Columns.GridColumn column, object val, CancelEventArgs ce)
        {
            if (rowHandle < 0)
                return;
            if (column.FieldName == "Qty" && val.ToStr() != "")
            {
                DataTable dtZorunluAdet = dsBOMLines.Tables[2];
                int ProductCategoryId = gvBOMLines.GetRowCellValue(rowHandle, "ProductCategoryId").ToInt();
                int AksesuarId = gvBOMLines.GetRowCellValue(rowHandle, "AksesuarId").ToInt();

                bool IsProductCategoryExist = dtZorunluAdet.Select("ProductCategoryId = " + ProductCategoryId).Length > 0;
                bool IsOptionExist = dtZorunluAdet.Select("ProductCategoryId = " + ProductCategoryId + " and Isnull(Qty,0) = " + val.ToStr().Replace(",", ".")).Length > 0;

                ce.Cancel = (IsProductCategoryExist && !IsOptionExist) || AksesuarId == 0;
            }
        }

        RepositoryItemButtonEdit emptyEditor;
        void gvBOMLines_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.Column.FieldName == "Seri" &&
                view.GetRowCellDisplayText(e.RowHandle, "AksSeriId") == string.Empty)
                e.RepositoryItem = emptyEditor;

            else if (e.Column.FieldName == "Qty")
            {
                DataTable dtZorunluAdet = dsBOMLines.Tables[2];
                int ProductCategoryId = view.GetRowCellValue(e.RowHandle, "ProductCategoryId").ToInt();
                bool IsProductCategoryExist = dtZorunluAdet.Select("ProductCategoryId = " + ProductCategoryId).Length > 0;
                if (IsProductCategoryExist)
                {
                    List<string> adet = new List<string>();
                    foreach (DataRow dr in dtZorunluAdet.Select("ProductCategoryId = " + ProductCategoryId))
                        adet.Add(dr["Qty"].ToDecimal().ToString("#,##0.###"));

                    RepositoryItemComboBox riAdet = new RepositoryItemComboBox();
                    riAdet.Items.AddRange(adet);
                    riAdet.TextEditStyle = TextEditStyles.DisableTextEditor;
                    e.RepositoryItem = riAdet;
                }
            }
        }

        void gvBOMLines_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView view = sender as GridView;

            int AksesuarId = view.GetRowCellValue(view.FocusedRowHandle, "AksesuarId").ToInt();
            e.Cancel = view.FocusedColumn.FieldName == "Qty" && AksesuarId == 0;
        }

        void gvBOMLines_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                int AksesuarId = gvBOMLines.GetRowCellValue(e.RowHandle, "AksesuarId").ToInt();
                if (AksesuarId > 0)
                {
                    switch(e.Column.FieldName)
                    {
                        case "Tip":
                            string Tip = gvBOMLines.GetRowCellValue(e.RowHandle, "Tip").ToStr();
                            if (Tip == "")
                                e.Appearance.BackColor = Color.LightPink;
                            break;
                        case "Qty":
                            int Qty = gvBOMLines.GetRowCellValue(e.RowHandle, "Qty").ToInt();
                            if (Qty == 0)
                                e.Appearance.BackColor = Color.LightPink;
                            break;
                    }
                }
            }
        }

        void bbiSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvBOMLines.EndEditing();

            DataTable dtBOM = gvBOMLines.DataTable();
            bool IsQtyNull = dtBOM.Select("AksesuarId is not null and Isnull(Qty,0) = 0").Length > 0;
            if (IsQtyNull)
            {
                ShowErrorMessage("Adet boş olamaz !");
                return;
            }

            bool IsTypeNull = dtBOM.Select("AksesuarId is not null and Tip is null").Length > 0;
            if (IsTypeNull)
            {
                ShowErrorMessage("Tip Seçin !");
                return;
            }

            string xml = gvBOMLines.Serialize();
            ExecuteNonQuery("InsWBBomLines", xml, idx, WhoAmI);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
